﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using System.Data.SqlClient;
using rock.UI.common;
using rock.UI.houseinfo;
using rock.UI.fee;

namespace rock.UI.pictureinfo.subview
{
    public partial class UserControlHouse : UserControl
    {

        private Color statusColor = System.Drawing.Color.Pink;
        private int houseStatus =Constant.HOUSE_ROOM_STATUS_NORECORD;
        private String aHOUSE_CODE;
        private String aHOUSE_TEXT;

        public String _HOUSE_TEXT
        {
            get
            {
                return aHOUSE_TEXT;
            }
            set
            {
                this.aHOUSE_TEXT = value;
              //  this.label1.Text = this.aHOUSE_TEXT;
            }

        }
        public String _HOUSE_CODE {
            get {
                return aHOUSE_CODE;
            }
            set {
                this.aHOUSE_CODE = value;
            }

        }


        private String aXIAOQU;
        public String _AXIAOQU
        {
            get
            {
                return aXIAOQU;
            }
            set
            {
                this.aXIAOQU = value;
            }
        }

        private String aADONG;
        public String _ADONG
        {
            get
            {
                return aADONG;
            }
            set
            {
                this.aADONG = value;
            }
        }

        private int aASEQID;
        public int _ASEQID
        {
            get
            {
                return aASEQID;
            }
            set
            {
                this.aASEQID = value;
                this.label1.Text = this.aASEQID+"";
            }
        }

        private String aAB;
        public String _AB
        {
            get
            {
                return aAB;
            }
            set
            {
                this.aAB = value;
            }
        }


        public UserControlHouse()
        {
            InitializeComponent();
        }

        private void UserControlHouse_Load(object sender, EventArgs e)
        {
          
        }


        public void inits() {

            String realValue = "";//i < 10 ? ("0" + i) : i + ""; //"B1-401A"; header B1-4 realValue 01 A tail
            if (this._ASEQID < 10)
            {
                realValue = "00" + this._ASEQID;
            }
            else if (this._ASEQID >= 10 && this._ASEQID < 100)
            {
                realValue = "0" + this._ASEQID;
            }
            else
            {
                realValue = "" + this._ASEQID;
            }

            this._HOUSE_CODE = "" + this._AXIAOQU + "-" + this._ADONG + realValue+ this._AB;

            //SQL LOAD DETAIL
            if (this._HOUSE_CODE != null && !this._HOUSE_CODE.Equals(""))
            {
                String sql = @"SELECT STATUS  FROM HOUSE_ROOM WHERE  HOUSE_ROOM.HOUSE_CODE = '{0}'";

                DataSet statusDs = DbHelperSQL.Query(String.Format(sql, this._HOUSE_CODE));

                if (statusDs != null && statusDs.Tables.Count > 0 && statusDs.Tables[0].Rows.Count > 0)
                {
                    int status = (int)statusDs.Tables[0].Rows[0]["STATUS"];
                    houseStatus = status;
                }
                else
                {
                    //没有找到房子
                    houseStatus = Constant.HOUSE_ROOM_STATUS_NORECORD;
                }
            }
            else
            {
                houseStatus = Constant.HOUSE_ROOM_STATUS_NOHOUSE_CODE;
            }
            setStatusColor();
            //SET STATUS.
        }


        private void label1_Click(object sender, EventArgs e)
        {
                //显示详情
              showDetailToolStripMenuItem_Click(  sender,   e);
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            this.label1.BackColor = System.Drawing.SystemColors.ActiveBorder;
        }

        private void label1_MouseEnter(object sender, EventArgs e)
        {
            this.label1.BackColor = System.Drawing.SystemColors.AppWorkspace;
        }

        private void label1_MouseLeave(object sender, EventArgs e)
        {
            this.label1.BackColor = statusColor;
        }

        private void label1_MouseUp(object sender, MouseEventArgs e)
        {
            this.label1.BackColor = statusColor;
        }



        private void setStatusColor() {
 
            switch (houseStatus)
            {
                case Constant.HOUSE_ROOM_STATUS_UNRENT :
                    statusColor = System.Drawing.Color.DodgerBlue;
                    break;
                case Constant.HOUSE_ROOM_STATUS_RENTED:
                    statusColor = System.Drawing.Color.Green;
                    break;
                case Constant.HOUSE_ROOM_STATUS_YUDING:
                    statusColor = System.Drawing.Color.Yellow;
                    break;
                case Constant.HOUSE_ROOM_STATUS_PAUSE:
                    statusColor = System.Drawing.Color.LightSeaGreen;
                    break;
                case Constant.HOUSE_ROOM_STATUS_NORECORD:
                    statusColor = System.Drawing.Color.Silver;
                    break;
                case Constant.HOUSE_ROOM_STATUS_NOHOUSE_CODE:
                    statusColor = System.Drawing.Color.Orange;
                    break;
                default:
                    statusColor = System.Drawing.Color.Pink;
                    break;

            }
            this.label1.BackColor = statusColor;
            if (this._HOUSE_CODE != null && this._HOUSE_CODE.Length == 7)
            {
                this.label1.Text = this._HOUSE_CODE.Substring(3, 4);
            }
            else if (this._HOUSE_CODE != null && this._HOUSE_CODE.Length == 8)
            { 
                this.label1.Text = this._HOUSE_CODE.Substring(3, 5);
            }
           
        }
        /// <summary>
        /// 显示详情
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void showDetailToolStripMenuItem_Click(object sender, EventArgs e)
        {
             if( houseStatus == Constant.HOUSE_ROOM_STATUS_UNRENT
                 ||houseStatus == Constant.HOUSE_ROOM_STATUS_RENTED
                 ||houseStatus == Constant.HOUSE_ROOM_STATUS_YUDING
                 ||houseStatus == Constant.HOUSE_ROOM_STATUS_PAUSE
                 ){
                String sql = @"SELECT SEQID , STATUS  FROM HOUSE_ROOM WHERE  HOUSE_ROOM.HOUSE_CODE = '{0}'";
                DataSet statusDs = DbHelperSQL.Query(String.Format(sql, this._HOUSE_CODE));

                if (statusDs != null && statusDs.Tables.Count > 0 && statusDs.Tables[0].Rows.Count > 0)
                {
                    int seqid = (int)statusDs.Tables[0].Rows[0]["SEQID"];
                    Form1HouseDetailInfo form = new Form1HouseDetailInfo();
                    form._ROOM_SEQID = seqid;
                    form.userControlRoomAllInfo1._ROOM_SEQID =   seqid;
                    form.ShowDialog(this.FindForm());
                }
                else {
                    MessageBox.Show("没有找到数据库记录信息.", "提示");
                }
           }else{
               MessageBox.Show("没有找到房间信息,请先点击页面顶部的刷新按钮刷新数据.", "提示");
           }

        }

        private void RentalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(houseStatus == Constant.HOUSE_ROOM_STATUS_UNRENT){
                 String sql = @"SELECT SEQID , STATUS  FROM HOUSE_ROOM WHERE  HOUSE_ROOM.HOUSE_CODE = '{0}'";

                DataSet statusDs = DbHelperSQL.Query(String.Format(sql, this._HOUSE_CODE));
            
                 if (statusDs != null && statusDs.Tables.Count > 0 && statusDs.Tables[0].Rows.Count > 0)
                {
                    int status = (int)statusDs.Tables[0].Rows[0]["STATUS"];
                    int seqid = (int)statusDs.Tables[0].Rows[0]["SEQID"];
                     if (status != Constant.HOUSE_ROOM_STATUS_UNRENT)
                    {
                        MessageBox.Show("选择的房间目前的状态不可以租赁.", "提示");
                        return;
                    }
                    FormRentContractUpdate fcv = new FormRentContractUpdate();
                    fcv.oneRefreshParentData = reloadStatus;
                    fcv._ModelRENT_CONTRACT.HOUSE_ROOM_SEQID = Convert.ToInt32(seqid);
                    fcv.ShowDialog(this);
                    fcv.oneRefreshParentData = reloadStatus;
                }
                else
                {
                    //没有找到房子
                   // houseStatus = Constant.HOUSE_ROOM_STATUS_NORECORD;
                    MessageBox.Show("没有找到房间信息.", "提示");
                }
            
            }else{
                MessageBox.Show("此房间状态不能租用.", "提示");
            }

        }

        private void ShaoJujingToolStripMenuItem_Click(object sender, EventArgs e)
        {


            if (houseStatus == Constant.HOUSE_ROOM_STATUS_RENTED)
            {
                String sql = @"SELECT  RENT_CONTRACT.SEQID AS CONTRACT_SEQID ,RENT_CUSTOM_SEQID  FROM RENT_CONTRACT
                            LEFT JOIN HOUSE_ROOM ON RENT_CONTRACT.HOUSE_ROOM_SEQID = HOUSE_ROOM.SEQID
                            WHERE RENT_CONTRACT.STATUS = 1 AND HOUSE_ROOM.HOUSE_CODE = '{0}'";

                DataSet statusDs = DbHelperSQL.Query(String.Format(sql, this._HOUSE_CODE));

                if (statusDs != null && statusDs.Tables.Count > 0 && statusDs.Tables[0].Rows.Count > 0)
                {
                    
                    int CONTRACT_SEQID = (int)statusDs.Tables[0].Rows[0]["CONTRACT_SEQID"];
                    int CUSTOM_SEQID = (int)statusDs.Tables[0].Rows[0]["RENT_CUSTOM_SEQID"];
                    FormFeeShaoRoomZhujing ffsz = new FormFeeShaoRoomZhujing();
               
                    ffsz._ModelFEE_ROOM.CONTRACT_SEQID =CONTRACT_SEQID;
                    ffsz._ModelFEE_ROOM.CUSTOM_SEQID = CUSTOM_SEQID;
                    ffsz.oneRefreshParentData = this.reloadStatus;
                    ffsz.ShowDialog(this);


                    //注册代理
                }
                else
                {
                    //没有找到房子
                    // houseStatus = Constant.HOUSE_ROOM_STATUS_NORECORD;
                    MessageBox.Show("没有找到房间信息.", "提示");
                }

            }
            else
            {
                MessageBox.Show("此房间状态无需交租.", "提示");
            }



            
        }

        private void ShaoOtherfeeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (houseStatus == Constant.HOUSE_ROOM_STATUS_RENTED)
            {
                String sql = @"SELECT  RENT_CONTRACT.SEQID AS CONTRACT_SEQID ,RENT_CUSTOM_SEQID  FROM RENT_CONTRACT
                            LEFT JOIN HOUSE_ROOM ON RENT_CONTRACT.HOUSE_ROOM_SEQID = HOUSE_ROOM.SEQID
                            WHERE RENT_CONTRACT.STATUS = 1 AND HOUSE_ROOM.HOUSE_CODE = '{0}'";

                DataSet statusDs = DbHelperSQL.Query(String.Format(sql, this._HOUSE_CODE));

                if (statusDs != null && statusDs.Tables.Count > 0 && statusDs.Tables[0].Rows.Count > 0)
                {
                  
                    int CONTRACT_SEQID = (int)statusDs.Tables[0].Rows[0]["CONTRACT_SEQID"];
                    int CUSTOM_SEQID = (int)statusDs.Tables[0].Rows[0]["RENT_CUSTOM_SEQID"];
                    FormFeeShaoOtherMoney ffsz = new FormFeeShaoOtherMoney();
                    
                    ffsz._ModelFEE_OTHER.CONTRACT_SEQID = CONTRACT_SEQID;
                    ffsz._ModelFEE_OTHER.CUSTOM_SEQID = CUSTOM_SEQID;
                    ffsz.oneRefreshParentData = this.reloadStatus;
                    ffsz.ShowDialog(this);
                    //注册代理
                }
                else
                {
                    //没有找到房子
                    // houseStatus = Constant.HOUSE_ROOM_STATUS_NORECORD;
                    MessageBox.Show("没有找到房间信息.", "提示");
                }

            }
            else
            {
                MessageBox.Show("此房间状态无需交其它费用.", "提示");
            }

        }
        private void reloadStatus (int notuse){
            inits();
        }
        
    }
}
